/**
 * mapkey
 */
export const mapKey = "65YBZ-FZSHF-SQTJC-NS7QB-EWK57-S4BRU";

/**
 * 地图是否已经加载了
 */
let isMapLoad = false;

/**
 * 是否在加载中
 */
let isMapLoading = false;

/**
 * 缓存操作
 */
let cacheList: any[] = [];

/**
 * 加载地图组件
 */
function mapLoad() {
    if (isMapLoad || isMapLoading) return;
    isMapLoading = true;
    //创建script标签
    var script = document.createElement("script");
    //设置标签的type属性
    script.type = "text/javascript";
    //设置标签的链接地址
    script.src = `https://map.qq.com/api/js?v=2.exp&key=${mapKey}&callback=onMapLoad`;
    //添加标签到dom
    document.body.appendChild(script);
}

/**
 * 当地图加载完成的时候
 */
export function onMapInit(callback: any) {
    if (isMapLoad) {
        callback && callback();
    } else {
        mapLoad();
        cacheList.push(callback);
    }
}

//加载到全局
(window as any).onMapLoad = function () {
    isMapLoad = true;
    isMapLoading = false;
    cacheList.forEach(onMapInit);
    cacheList.length = 0;
};